import { Controller, Get, Post, Body, Param, Delete, Query } from '@nestjs/common';
import { PointsService } from './points.service';
import { CreatePointsRecordDto } from './dto/create-points-record.dto';

@Controller('points')
export class PointsController {
  constructor(private readonly pointsService: PointsService) {}

  @Post()
  async create(@Body() createPointsRecordDto: CreatePointsRecordDto) {
    const record = await this.pointsService.create(createPointsRecordDto);
    return {
      code: 200,
      data: record,
      message: '积分记录创建成功',
    };
  }

  @Get()
  async findAll(@Query() query) {
    const [records, total] = await this.pointsService.findAll(query);
    return {
      code: 200,
      data: {
        list: records,
        total,
        page: +query.page || 1,
        limit: +query.limit || 10,
      },
      message: '获取积分记录列表成功',
    };
  }

  @Get('user/:userId')
  async findByUserId(@Param('userId') userId: string, @Query() query) {
    const [records, total] = await this.pointsService.findByUserId(+userId, query);
    return {
      code: 200,
      data: {
        list: records,
        total,
        page: +query.page || 1,
        limit: +query.limit || 10,
      },
      message: '获取用户积分记录列表成功',
    };
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    const record = await this.pointsService.findOne(+id);
    return {
      code: 200,
      data: record,
      message: '获取积分记录详情成功',
    };
  }

  @Delete(':id')
  async remove(@Param('id') id: string) {
    await this.pointsService.remove(+id);
    return {
      code: 200,
      message: '积分记录删除成功',
    };
  }
} 